﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfClient.Services;
using Domain;
using EditableBusinessObjects.Postsharp.Public;
using System.ComponentModel;

namespace WpfClient
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {

        PersonViewModel vm;

        public Window1()
        {
            InitializeComponent();

            vm = new PersonViewModel();
            SchoolService svc = new SchoolService();

            vm.AllCourses = svc.GetCourses();
            vm.RegisterDepartments(svc.GetDepartments());

            this.DataContext = vm;
        }

        private void Search(object sender, RoutedEventArgs e)
        {
            SchoolService svc = new SchoolService();
            Person p = svc.GetPerson(Int32.Parse(IDtb.Text), ckGetEnrollments.IsChecked.GetValueOrDefault(false), ckCourses.IsChecked.GetValueOrDefault(false));

            vm.SelectedPerson = p;

            // the objects in department list are different from the department in the courses

        }

        private void BeginEdit(object sender, RoutedEventArgs e)
        {
            ((IEditableBusinessObject)this.vm.SelectedPerson).BeginEdit();
        }

        private void CancelEdit(object sender, RoutedEventArgs e)
        {
            ((IEditableBusinessObject)this.vm.SelectedPerson).CancelEdit();
        }

        private void EndEdit(object sender, RoutedEventArgs e)
        {
            ((IEditableBusinessObject)this.vm.SelectedPerson).EndEdit();

        }

        private void SaveGraph(object sender, RoutedEventArgs e)
        {
            SchoolService svc = new SchoolService();
            svc.SavePerson(vm.SelectedPerson);
        }

        private void RemoveCourse(object sender, RoutedEventArgs e)
        {
            Course course = (Course) this.cmbCourses.SelectedItem;

            Person p = vm.SelectedPerson;

            course.Persons.Remove(p);
            p.Courses.Remove(course);

            this.DataContext = null;
            this.DataContext = vm;
        }

        private void AddCourse(object sender, RoutedEventArgs e)
        {
            Person p = vm.SelectedPerson;

            Course newCourse = new Course();
            newCourse.Persons = new List<Person>();
            newCourse.Persons.Add(p);
            newCourse.Enrollments = new List<Enrollment>();

            p.Courses.Add(newCourse);

            this.DataContext = null;
            this.DataContext = vm;
        }

        private void RemoveEnrollment(object sender, RoutedEventArgs e)
        {
            Enrollment enrollment = (Enrollment)this.cmbEnrollments.SelectedItem;

            Person p = vm.SelectedPerson;

            enrollment.Person = null;
            p.Enrollments.Remove(enrollment);

            this.DataContext = null;
            this.DataContext = vm;
        }

        private void ChangedDepartment(object sender, SelectionChangedEventArgs e)
        {
            foreach (Department old in e.RemovedItems)
            {
                old.Courses.Remove((Course)(((ComboBox)sender).Tag));
            }

            foreach (Department newDP in e.AddedItems)
            {
                if (!newDP.Courses.Contains((Course)(((ComboBox)sender).Tag)))
                {
                    newDP.Courses.Add((Course)(((ComboBox)sender).Tag));
                }
            }
        }



    }

    class PersonViewModel : INotifyPropertyChanged
    {
        Person selectedPerson;
        public Person SelectedPerson
        {
            get { return selectedPerson; }
            set { 
                selectedPerson = value;

                List<Department> deps = new List<Department>();
                // create a list of departments with the current department objects
                foreach (Course c in selectedPerson.Courses)
                {
                    deps.Add(c.Department);                    
                }

                foreach (Department d in OriginalDepartmentList)
                {
                    if (!deps.Any(dp => dp.DepartmentID == d.DepartmentID)  )
                        deps.Add(d);
                }

                AllDepartments = deps;

                PropertyChanged(this, new PropertyChangedEventArgs("SelectedPerson"));
            
            }
        }

        public IList<Course> AllCourses { get; set; }

        public IList<Department> AllDepartments { get; set; }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        #endregion
        IList<Department> OriginalDepartmentList;
        internal void RegisterDepartments(IList<Department> deps)
        {
            OriginalDepartmentList = deps;
        }
    }
}
